<template>
  <div class="Grade">
    <el-row display="margin-top:10px">
        <el-input v-model="input" placeholder="请输入班级名" style="display:inline-table; width: 30%; float:left"></el-input>
        <el-button type="primary" @click="addGrade()" style="float:left; margin: 2px;">新增</el-button>
    </el-row>
    <el-row>
        <el-table :data="gradeList" style="width: 100%" border>
          <el-table-column prop="id" label="班级编号" min-width="100">
            <template scope="scope"> {{ scope.row.pk }} </template>
          </el-table-column>
          <el-table-column prop="grade_name" label="班级名称" min-width="100">
            <template scope="scope"> {{ scope.row.fields.grade_name }} </template>
          </el-table-column>
          el-table-column prop="boys_num" label="男生人数" min-width="100">
            <template scope="scope"> {{ scope.row.fields.boys_num }} </template>
          </el-table-column>
           el-table-column prop="girls_num" label="女生人数" min-width="100">
            <template scope="scope"> {{ scope.row.fields.girls_num }} </template>
          </el-table-column>
           el-table-column prop="content" label="班级简介" min-width="100">
            <template scope="scope"> {{ scope.row.fields.content }} </template>
          </el-table-column>
           el-table-column prop="class_teacher" label="班主任" min-width="100">
            <template scope="scope"> {{ scope.row.fields.class_teacher }} </template>
          </el-table-column>
        </el-table>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'Grade',
  data () {
    return {
      input: '',
      gradeList: []
    }
  },
  mounted: function () {
    this.showGrades()
  },
  methods: {
    addGrade () {
      this.$http.get('http://127.0.0.1:8000/add_grade?grade_name=' + this.input)
        .then((response) => {
          var res = JSON.parse(response.bodyText)
          if (res.error_num === 0) {
            this.showGrades()
          } else {
            this.$message.error('新增班级失败，请重试')
            console.log(res['msg'])
          }
        })
    },
    showGrades () {
      this.$http.get('http://127.0.0.1:8000/show_grades')
        .then((response) => {
          var res = JSON.parse(response.bodyText)
          console.log(res)
          if (res.error_num === 0) {
            this.gradeList = res['list']
          } else {
            this.$message.error('查询班级失败')
            console.log(res['msg'])
          }
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>
